<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <a-button type="primary" @click="handleForm(false, {})" v-auth="'sys:mobileMenu:save'">
          <Icon icon="i-ant-design:plus-outlined" /> {{ t("新增") }}
        </a-button>
        <a-button type="primary" preIcon="ant-design:edit-outlined" @click="handleOpenGroupList">菜单组</a-button>
      </template>

      <!-- <template #firstColumn="{ record }">
        <span class="cursor-pointer" @click="expandCollapse(record)">
          {{ record.areaName }}
        </span>
      </template> -->
    </BasicTable>
    <InputForm @register="registerInputFormDrawer" @success="handleSuccess" />
    <FormDetail @register="registerFormDetail" />
    <MyDataGroupList @register="registerGroupList" @success="reload" :bizType="DataGroupBizType.MOBILE_MENU" />
  </div>
</template>
<script lang="ts" name="mobile-menu" setup>
  import { ref } from "vue"
  import { BasicTable, BasicColumn, useTable } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import { useI18n } from "/@/hooks/web/useI18n"
  import { useGlobSetting } from "/@/hooks/setting"
  import { columns, searchFormSchema } from "./data"
  import { listMobileMenu, deleteMobileMenu, deleteGroup, saveOrUpdateGroup, batchDeleteGroup } from "/@/api/sys/mobileMenu"
  import { useMessage } from "/@/hooks/web/useMessage"
  import InputForm from "./Form.vue"
  import FormDetail from "./Detail.vue"
  import { useRouter } from "vue-router"
  import { Icon } from "/@/components/Icon"
  import { MyDataGroupList, DataGroupBizType } from "/@/components/BizDataGroup/index"
  import { useSelectMaterial } from "/@/components/BizMaterial/index"
  useSelectMaterial()

  const { t } = useI18n("")
  const { createMessage } = useMessage()
  const glob = useGlobSetting()
  const [registerInputFormDrawer, { openDrawer: openInputFormDrawer }] = useDrawer()
  const [registerFormDetail, { openDrawer: openFormDetail }] = useDrawer()
  const [registerGroupList, { openDrawer: openGroupList }] = useDrawer()

  const actionColumn: BasicColumn = {
    width: 140,
    title: "操作",
    dataIndex: "action",
    fixed: "right",
    slot: "tableActions",
    actions: (record: Recordable) => [
      {
        label: "删除",
        color: "error",
        ifShow: record.sendStatus != 1,
        auth: "sys:mobileMenu:delete",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        // auth: 'sys:area:update',
        onClick: handleForm.bind(null, true, record),
      },
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      },
    ],
    dropDownActions: (record: Recordable) => [],
  }

  const [registerTable, { reload, expandAll, collapseAll, expandCollapse }] = useTable({
    title: "移动端菜单",
    api: listMobileMenu,
    columns: columns,
    bordered: false,
    beforeFetch: (params) => {
      return params
    },
    formConfig: {
      baseColProps: { lg: 6, md: 12 },
      labelWidth: 90,
      schemas: searchFormSchema,
      showResetButton: false,
    },
    showTableSetting: true,
    useSearchForm: true,
    isTreeTable: true,
    pagination: true,
    canResize: true,
    rowKey: "id",
    actionColumn,
  })

  async function handleDelete(record) {
    await deleteMobileMenu({ id: record.id }, reload)
  }

  const handleForm = (isUpdate, record) => {
    openInputFormDrawer(true, { record, isUpdate })
  }

  const handleDetail = (record) => openFormDetail(true, { record })

  const handleOpenGroupList = () => {
    openGroupList(true, {
      title: "菜单组",
      width: 800,
      groupCodeRequired: true,
      deleteApi: deleteGroup,
      batchDeleteApi: batchDeleteGroup,
      saveOrUpdateApi: saveOrUpdateGroup,
    })
  }
  const handleSuccess = () => {
    reload()
  }
</script>
